package cn.hhhy.service.impl;

import cn.hhhy.mapper.OrderMapper;
import cn.hhhy.mapper.ParkingMapper;
import cn.hhhy.pojo.Order;
import cn.hhhy.pojo.PageResult;
import cn.hhhy.pojo.Parking;
import cn.hhhy.pojo.QueryPageBean;
import cn.hhhy.service.OrderService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ParkingMapper parkingMapper;

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();

        IPage<Order> page = orderMapper.selectPage(new Page<>(currentPage,pageSize),
                Wrappers.lambdaQuery(Order.class)
                        .like(!Strings.isEmpty(queryString), Order::getUid, queryString)
                        .or()
                        .like(!Strings.isEmpty(queryString), Order::getOid, queryString)
        );
        return new PageResult(page.getTotal(), page.getRecords());
    }

    @Override
    public Order findById(Integer id) {
        return orderMapper.selectById(id);
    }

    @Override
    public void add(Order order) {
        order.setOtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        orderMapper.insert(order);
    }

    @Override
    public void delete(Integer id) {
        orderMapper.deleteById(id);
    }

    @Override
    public void update(Order order) {
        orderMapper.updateById(order);
    }

    @Override
    public void putOrder(Map<String, Object> orderInfo) {
        Integer uid = (Integer) orderInfo.get("uid");
        Integer vid = (Integer) orderInfo.get("vid");
        Integer pid = (Integer) orderInfo.get("pid");
        Double price =  Double.valueOf(orderInfo.get("price").toString());
//        创建订单
        Order order = new Order();
        order.setUid(uid);
        order.setPid(pid);
        order.setVid(vid);
        order.setPrice(price);
        order.setOtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        order.setState(0);
//        车位状态设置
        Parking parking = parkingMapper.selectById(pid);
        parking.setState(0);
        parkingMapper.updateById(parking);
        orderMapper.insert(order);
    }

    @Override
    public List<Order> findByUID(Integer id) {
        return orderMapper.selectList(Wrappers.lambdaQuery(Order.class).eq(Order::getUid,id));
    }

    @Override
    public Order pay(Integer id) {
        Order order = orderMapper.selectById(id);
        order.setState(1); // 交易成功
        orderMapper.updateById(order);
        Parking parking = parkingMapper.selectById(order.getPid());
        parking.setState(-1);
        parking.setUid(order.getUid());
        parkingMapper.updateById(parking);
        return order;
    }
}
